package com.jiangjiang.eshop.purchase.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jiangjiang.eshop.purchase.constant.PurchaseOrderStatus;
import com.jiangjiang.eshop.purchase.dto.PurchaseOrderApproveDTO;
import com.jiangjiang.eshop.purchase.dto.PurchaseOrderDTO;
import com.jiangjiang.eshop.purchase.dto.PurchaseOrderPageQuery;
import com.jiangjiang.eshop.purchase.service.PurchaseOrderService;
import com.jiangjiang.eshop.wms.constant.PurchaseInputOrderApproveResult;
import com.jiangjiang.eshop.wms.constant.PurchaseInputOrderStatus;
import com.sun.xml.bind.v2.model.core.ID;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @ClassName PurchaseOrderController
 * @Description TODO
 * @Author jhw
 * @Date 2021/9/20 7:56
 * @Version 1.0
 **/
@Slf4j
@RestController
@RequestMapping("/purchase/order")
public class PurchaseOrderController {

    @Autowired
    private PurchaseOrderService purchaseOrderService;

    /**
     * 采购单保存
     *
     * @param dto 采购单数据
     * @return 处理结果
     */
    @PostMapping("/savePurchaseOrder")
    public Boolean savePurchaseOrder(@RequestBody PurchaseOrderDTO dto) {
        try {
            return purchaseOrderService.savePurchaseOrder(dto);
        }catch (Exception e){
            log.warn("PurchaseOrderController savePurchaseOrder error",e);
        }
        return false;
    }

    /**
     * 采购单保存
     *
     * @param dto 采购单数据
     * @return 处理结果
     */
    @PostMapping("/updatePurchaseOrder")
    public Boolean updatePurchaseOrder(@RequestBody PurchaseOrderDTO dto) {
        try {
            return purchaseOrderService.updatePurchaseOrder(dto);
        }catch (Exception e){
            log.warn("PurchaseOrderController updatePurchaseOrder error",e);
        }
        return false;
    }

    /**
     * 根据采购单id查询采购单数据
     *
     * @param purchaseOrderId 采购单数据
     * @return 处理结果
     */
    @GetMapping("/queryById")
    public PurchaseOrderDTO queryById(@RequestParam("purchaseOrderId") Long purchaseOrderId) {
        try {
            return purchaseOrderService.queryById(purchaseOrderId);
        }catch (Exception e){
            log.warn("PurchaseOrderController queryById error",e);
        }
        return null;
    }

    /**
     * 分页查询采购单数据
     *
     * @param query 采购单数据
     * @return 查询结果
     */
    @PostMapping("/queryForPage")
    public Page<PurchaseOrderDTO> queryForPage(@RequestBody  PurchaseOrderPageQuery query) {
        try {
            return purchaseOrderService.queryForPage(query);
        }catch (Exception e){
            log.warn("PurchaseOrderController queryForPage error",e);
        }
        return null;
    }




    /**
     * 采购单提交审核
     *
     * @param id 采购单id
     * @return 处理结果
     */
    @GetMapping("/submit/approve/{id}")
    public Boolean submitApprove(@PathVariable Long id) {
        try {
            return purchaseOrderService.submitApprove(id);
        }catch (Exception e){
            log.warn("PurchaseOrderController submitApprove error",e);
        }
        return false;
    }

    /**
     * 采购单提交审核
     *
     * @param dto 入参
     * @return 处理结果
     */
    @PostMapping("/approve")
    public Boolean approve(@RequestBody PurchaseOrderApproveDTO dto) {
        try {
            return purchaseOrderService.approve(dto.getPurchaseOrderId(),dto.getApproveResult());
        }catch (Exception e){
            log.warn("PurchaseOrderController approve error",e);
        }
        return false;

    }
}
